﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;
using AjaxPro;
using System.Data.SqlClient;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem.HuanTong
{
    public partial class HTUserSelect : System.Web.UI.Page
    {
        DataTable TreeInfo;
        TreeNode trNewNode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                BindTree();
                BindGridView();
                DropDownListBind(string.Empty);
            }
        }

        #region DropDownListBind
        public void DropDownListBind(string Deparatment)
        {
            //行业类别一
            ddlIndustryType1.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("14", "%");
            ddlIndustryType1.DataBind();
            ddlIndustryType1.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlIndustryType1.Items.Insert(1, new ListItem("全部", "全部"));
            //环评类别
            ddlhuanpingleibie.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("7");
            ddlhuanpingleibie.DataBind();
            ddlhuanpingleibie.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlhuanpingleibie.Items.Insert(1, new ListItem("全部", "全部"));
            //项目组织人
            //ddlZuZhiRen.DataSource = EnvironmentManagerSystem.BLL.XIANGMU.GetFuzeren(Deparatment);
            //ddlZuZhiRen.DataTextField = "UserName";
            //ddlZuZhiRen.DataValueField = "ID";
            //ddlZuZhiRen.DataBind();
            //ddlZuZhiRen.Items.Insert(0, new ListItem("请选择", "请选择"));
            //ddlZuZhiRen.Items.Insert(1, new ListItem("全部", "全部"));
            //环评单位
            ddlhp.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.GetList();
            ddlhp.DataBind();
            ddlhp.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlhp.Items.Insert(1, new ListItem("全部", "全部"));
            //项目执行部门
            ddlfenpeizhixingbumen.DataSource = EnvironmentManagerSystem.BLL.ren.GetList();
            ddlfenpeizhixingbumen.DataTextField = "username";
            ddlfenpeizhixingbumen.DataValueField = "ID";
            ddlfenpeizhixingbumen.DataBind();
            ddlfenpeizhixingbumen.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlfenpeizhixingbumen.Items.Insert(1, new ListItem("全部", "全部"));
            //地区代码1
            ddlAddressCode0.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("30", "%00");
            ddlAddressCode0.DataBind();
            ddlAddressCode0.Items.Insert(0, new ListItem("请选择", "请选择"));
        }
        #endregion

        #region DropDownList_SelectedIndexChanged
        //行业类别一
        protected void ddlIndustryType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //行业类别2
            ddlIndustryType2.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("24", ddlIndustryType1.SelectedValue + "%");
            ddlIndustryType2.DataBind();
            ddlIndustryType2_SelectedIndexChanged(sender, e);
            ddlIndustryType2.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlIndustryType2.Items.Insert(1, new ListItem("全部", "全部"));

        }
        //行业类别二
        protected void ddlIndustryType2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //行业类别3
            ddlIndustryType3.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("25", ddlIndustryType2.SelectedValue + "%");
            ddlIndustryType3.DataBind();
            ddlIndustryType3_SelectedIndexChanged(sender, e);
            ddlIndustryType3.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlIndustryType3.Items.Insert(1, new ListItem("全部", "全部"));

        }
        //行业类别三
        protected void ddlIndustryType3_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        //地区代码1
        protected void ddlAddressCode0_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlAddressCode2.Items.Clear();
            //地区代码2
            DataTable dt = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("30", ddlAddressCode0.Text.Substring(0, ddlAddressCode0.Text.Length - 2) + "%");
            ddlAddressCode2.DataSource = dt;
            ddlAddressCode2.DataBind();
            ddlAddressCode2_SelectedIndexChanged(sender, e);
            ddlAddressCode2.Items.Insert(0, new ListItem("请选择", "请选择"));
        }

        //地区代码2
        protected void ddlAddressCode2_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        #endregion

        #region 按钮(搜索按钮、重置按钮)(x)
        //搜索按钮
        protected void lbtnSelect_Click(object sender, EventArgs e)
        {
            lbtnBind();
        }
        private void lbtnBind()
        {
            if (txtXiangMuShouLiShiJianK.Text == "" && txtXiangMuShouLiShiJianJ.Text != "")
            {
                Response.Write("<script language='javascript'>alert('请填写要查询的受理起始时间！');</script>");
            }
            if (txtXiangMuShouLiShiJianK.Text != "" && txtXiangMuShouLiShiJianJ.Text == "")
            {
                Response.Write("<script language='javascript'>alert('请填写要查询的受理结束时间！');</script>");
            }
            //if (txtTaChaRiQiK.Text == "" && txtTaChaRiQiJ.Text != "")
            //{
            //    Response.Write("<script language='javascript'>alert('请填写要查询的踏查起始时间！');</script>");
            //}
            //if (txtTaChaRiQiK.Text != "" && txtTaChaRiQiJ.Text == "")
            //{
            //    Response.Write("<script language='javascript'>alert('请填写要查询的踏查结束时间！');</script>");
            //}
            //if (txtHuiYiShiJianK.Text == "" && txtHuiYiShiJianJ.Text != "")
            //{
            //    Response.Write("<script language='javascript'>alert('请填写要查询的会议起始时间！');</script>");
            //}
            //if (txtHuiYiShiJianK.Text != "" && txtHuiYiShiJianJ.Text == "")
            //{
            //    Response.Write("<script language='javascript'>alert('请填写要查询的会议结束时间！');</script>");
            //}
            if (txtreportdateK.Text == "" && txtreportdatej.Text != "")
            {
                Response.Write("<script language='javascript'>alert('请填写要查询的项目签发起始时间！');</script>");
            }
            if (txtreportdateK.Text != "" && txtreportdatej.Text == "")
            {
                Response.Write("<script language='javascript'>alert('请填写要查询的项目签发结束时间！');</script>");
            }
            string QueryCondition = string.Empty;
            //txtTaChaRiQiK.Text, txtTaChaRiQiJ.Text, txtHuiYiShiJianK.Text, txtHuiYiShiJianJ.Text,ddlReceiveState.SelectedItem.Text,cbqueren.Checked, cblvsetongdao.Checked, cbshoulituihuishanchu.Checked, ddlZuZhiRen.SelectedItem.Text, ddlpingguyijianxiangmuzhuangtai.SelectedItem.Text, 
            GridView1.DataSource = NoticeService.GetSelectht(ref QueryCondition, txtXiangMuShouLiShiJianK.Text, txtXiangMuShouLiShiJianJ.Text, ddlIndustryType1.SelectedValue, ddlIndustryType2.SelectedValue, ddlIndustryType3.SelectedValue, ddlhuanpingleibie.SelectedValue, txtProjectName.Text, ddlhp.SelectedValue, ddlLiuCheng.SelectedItem.Text, ddlfenpeizhixingbumen.SelectedItem.Text, ddlAddressCode0.SelectedValue, ddlAddressCode2.SelectedValue, txthedinghetongjine.Text, ddlxiangmushoulishijian.SelectedItem.Text, txtreportdateK.Text, txtreportdatej.Text, cbReports.Checked, cbReportb.Checked, cbNoticeState.Checked, ddljinjizhuangtai.SelectedItem.Text);
            GridView1.DataBind();
            HiddenField1.Value = QueryCondition;
        }
        //重置按钮
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            foreach (Control con in this.form1.Controls)
            {
                if (con is TextBox)
                    ((TextBox)con).Text = string.Empty;
                else if (con is CheckBox)
                    ((CheckBox)con).Checked = false;
                else if (con is DropDownList)
                    ((DropDownList)con).SelectedIndex = -1;
            }
        }
        #endregion

        #region TreeView相关
        protected void Button1_Click(object sender, EventArgs e)
        {
            List<Hashtable> list = new List<Hashtable>();
            foreach (TreeNode node in TreeView1.Nodes)
            {
                BianLi(node, ref list);
            }
            if (list.Count > 0)
                TreeViewPanel.Export(list, HiddenField1.Value);
        }

        private void BianLi(TreeNode Pnode, ref List<Hashtable> list)
        {
            foreach (TreeNode node in Pnode.ChildNodes)
            {
                if (node.Checked)
                {
                    Hashtable hs = new Hashtable();
                    hs.Add(node.Text, node.Value.Split(':')[1]);
                    list.Add(hs);
                }
                else if (node.ChildNodes.Count > 1)
                {
                    for (int i = 0; i < node.ChildNodes.Count; i++)
                    {
                        BianLi(node.ChildNodes[i], ref list);
                    }
                }
            }
        }
        private void BindTree()
        {
            TreeInfo = TreeViewPanel.GetTree();
            foreach (DataRow drRow in TreeInfo.Rows)
            {
                if (drRow["pid"].ToString().Trim() == "0")
                {
                    trNewNode = new TreeNode(drRow["text"].ToString().Trim(), drRow["id"].ToString().Trim());
                    trNewNode.SelectAction = TreeNodeSelectAction.Expand;
                    this.TreeView1.Nodes.Add(trNewNode);
                    if (drRow["HavaChildNode"].ToString().Trim() == "y")
                    {
                        DataTable childtable = TreeInfo.Clone();
                        foreach (DataRow row in TreeInfo.Rows)
                        {
                            if (row["pid"].ToString().Trim() == drRow["id"].ToString().Trim())
                            {
                                DataRow newRow = childtable.NewRow();
                                newRow.ItemArray = row.ItemArray;
                                childtable.Rows.Add(newRow);
                            }
                        }
                        Make_ChildNode(trNewNode, childtable);
                    }
                }
            }
        }
        protected void Make_ChildNode(TreeNode node, DataTable dtTable)
        {
            foreach (DataRow drRow in dtTable.Rows)
            {
                trNewNode = new TreeNode(drRow["text"].ToString().Trim(), drRow["id"].ToString().Trim());
                node.ChildNodes.Add(trNewNode);
                if (drRow["HavaChildNode"].ToString().Trim() == "y")
                {
                    trNewNode.Expanded = false;
                    trNewNode.SelectAction = TreeNodeSelectAction.Expand;
                    trNewNode.ShowCheckBox = false;
                    DataTable childtable = TreeInfo.Clone();
                    foreach (DataRow row in TreeInfo.Rows)
                    {
                        if (row["pid"].ToString().Trim() == drRow["id"].ToString().Trim())
                        {
                            DataRow newRow = childtable.NewRow();
                            newRow.ItemArray = row.ItemArray;
                            childtable.Rows.Add(newRow);
                        }
                    }
                    Make_ChildNode(trNewNode, childtable);
                }
                else
                {
                    if (drRow["value"].ToString().Trim() != string.Empty)
                    {
                        trNewNode.Value += ":" + drRow["value"].ToString().Trim();
                        trNewNode.ShowCheckBox = true;
                    }
                }
            }
            TreeView1.ExpandDepth = 1;
        }
        #endregion

        #region GridView
        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向   
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];
            // 调用业务数据获取方法   
            DataTable dtBind = this.getDB();
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图   
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            // GridView绑定并显示数据   
            this.GridView1.DataSource = dtBind;
            this.GridView1.DataBind();
        }
        // DataTable 数据源     
        private DataTable getDB()
        {
            DataTable dt = HuanPingService.GetSelectAll();
            return dt;
        }
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;
                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }
        private void SortGridView(string sortExpression, string direction)
        {
            DataTable dt = getDB();
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + direction;
            GridView1.DataSource = dv; //将DataView绑定到GridView上
            GridView1.DataBind();
        }
        //处理GridView的排序事件
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (GridViewSortDirection == SortDirection.Ascending) //设置排序方向
            {
                GridViewSortDirection = SortDirection.Descending;
                SortGridView(sortExpression, " DESC");
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                SortGridView(sortExpression, " ASC");
            }
        }
        //鼠标滑动
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(GridView1);
        }
        #endregion

        #region LinkButton1_Click
        protected void LinkButton1_Click1(object sender, EventArgs e)
        {
            string IDlist = string.Empty;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)GridView1.Rows[i].Cells[0].FindControl("cbx");
                if (chk.Checked)
                    IDlist += chk.Value + ",";
            }
            if (IDlist != string.Empty)
            {
                Session["a"] = IDlist.Trim(',');
                string aaa = Session["a"].ToString();
            }
            else
                Session["a"] = null;
            //Page.RegisterStartupScript("", "<script>show()</script>");
        }
        #endregion
    }
}